package top.itaso.lib.widget.map.bean;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;


/**
 * @Description: 中国地图中一个省的实体对象
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Github https://github.com/itsdf07
 * @Date 2021/5/30
 */
public class ChinaProvinceItem {
    /**
     * SVG地图瞄点数据
     */
    private Path mapPath;
    /**
     * 绘制地图的颜色值
     */
    private int mapDrawColor;
    /**
     * 省份名称
     */
    private String provinceName;

    /**
     * @param path SVG地图瞄点数据
     */
    public ChinaProvinceItem(Path path) {
        this.mapPath = path;
    }

    /**
     * 绘制省份功能
     *
     * @param canvas
     * @param paint
     */
    public void draw(Canvas canvas, Paint paint, boolean isSelect) {
        if (isSelect) {
            //绘制背景
            paint.setStrokeWidth(3);
            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.FILL);
            //如果不关闭硬件加速，则该阴影设置将无效
            paint.setShadowLayer(12, 8, 8, 0xFF555555);

            canvas.drawPath(mapPath, paint);

            //绘制省份
            paint.clearShadowLayer();
            paint.setColor(mapDrawColor);
            paint.setStyle(Paint.Style.FILL);
            paint.setStrokeWidth(3);
        } else {
            paint.clearShadowLayer();
            //绘制省份的填充内容
            paint.setStrokeWidth(3);//设置绘制时笔触的宽度，当绘制的样式为 Stroke 或 StrokeAndFill 时使用
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(mapDrawColor);
            canvas.drawPath(mapPath, paint);

            //绘制省份的边界线
            paint.setStrokeWidth(1);
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.BLACK);
        }
        canvas.drawPath(mapPath, paint);
    }

    /**
     * 如果区域包含指定的点，则返回 true，即判断触摸的位置是否坐落在省份path上
     *
     * @param x
     * @param y
     * @return
     */
    public boolean isTouch(int x, int y) {
        RectF rectF = new RectF();
        //测量出mapPath对应的矩形边界值，即left、top、right、bottom
        mapPath.computeBounds(rectF, true);
        Region region = new Region();
        //裁剪出rectF矩形中不规则的mapPath形状
        region.setPath(mapPath, new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));

        return region.contains(x, y);
    }

    public Path getMapPath() {
        return mapPath;
    }

    public void setMapPath(Path mapPath) {
        this.mapPath = mapPath;
    }

    public int getMapDrawColor() {
        return mapDrawColor;
    }

    public void setMapDrawColor(int mapDrawColor) {
        this.mapDrawColor = mapDrawColor;
    }

    public String getProvinceName() {
        return provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }
}
